package com.wvmi.gmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wvmi.gmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000132671881";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCC3z2cxTttlVX2s9S8Kwtdcuf8ZwNr1UmRxGOtUzX+J0deKHYfHMHUxcuqhBHrIHnzwar/PSInuqpPAFxDnZiHbxfRcczqQJpIP2XkJj/oA/t6u7tftfx+mzgTnZfAJjxZS0dDZ4LKmBc/l3uY9Pg/m124g8ExvInKYHEmpNRb5bnfjQjD1lsBlMnurrhSt+VqrjvHtttjgVstAofbQpCMoB7bCCPNkUl2JMWetVs7+tgFCSLBIwsOTN+mqwPado+hu2KFPgJaFhcdyS552KhMXmiR/N3FwsJQ2xycVxkTv5sZZkX/KYwQ03VGiqBx5X2gb4SpCz/nr9G9E1/QFfFrAgMBAAECggEAYm8gu2KJ0WGKV9Vow+MoWDBOFsUwiJHTFQV+52X37WKDxmludvY9QBlJl8ZgZ9Z0g4f+Yp4Lq5A1+tfNjbnieLNxWcJbmzHeGUs+Cv1OAefE5cqAI3wRHvUGl5JoCkl4CL2bKzKW6GS+z8DTYPeYJNdqBZdr30ns5b/mO2espjUlqMSkUp9vsdDMqkYlhxzc2jub3Uq0MY/knCc6whwffPLNJs8iGY8dSvrsDPbZI6nerDNnqM4CybmvUvR3ah9MC9TItqDCKirVefVRutBSujxtVYesGbUxZvUf+m/h3iAT1WTeRtq2jZZDB9xO+Da5hnKLPWQm3kskalI1SlUIKQKBgQDukOhh31vDtdkMqe4B3yJssWIQBspJHM9Uvs2uDTR5gl3QatNhKtH1+gBs5TDXC+pI5Idf7SGD6EFlR4mH9Y2u6ZOoZ1cmSBMBtTvzA4RLp1IxfnfpagPRW66mMPzbwqbctcbQnbxUXbWnMDVGTU1SQKqv1c8Ua2eYYi80gEbabQKBgQCMb5gNW4t5JDVwlk0Tos5FuUo6Yk/1+SA1SRGrwhW0pms9k8ZCkomcdmxCifY93bEKKLpJgNMN0inIDTs90bYHK78Q6vD86KlztOltgWbTsjUDBc2vcISg97c57wW6+M78m/fPmjTd31mRYBa8WnEIdoK0eGpX5b1uVgg9Vy6UNwKBgQCVtoqfITTNVUmpENs0gXtxCsCnnhtEVrgmTrd1CwRrXMzTBrcwu1aYLxQlQo+r5OZaaFAsevwVc6PWk1ZT5F3N7vejL3cZpxGGeN3uW76iW5bRSLSn3sgWkpq/4O5OW5Gr4MesE9EkgMqG6VIFlG15uOtk/aC/B8gzcSp+oCHW4QKBgFHpEOXRnFMHFIhbkg8+6VLvlOTizMKqEOgje0QUCvb4Dhq3Y28W616wD8rrjw1cjdNgF++hTPiFuUrutLwE4kficynZ7r+EsLlFLtVpIWGeUp81tld6qFD+ywbmbBA26PubuPmEdwtmJ2bMhdZMZhtKjEfY6CXmYQvG8D1e6VMXAoGBANzW9uZaJLm0szkfUWFpEGX5JtWPC+LrnFXJ6S310l4ooqeOeLVTdxSRyXcjuyDlt/80EEh4TZu1FTLmedCRUir4gIlKtZo6fBx22xnmSsaYG380lrKzQNEhCGz8248KVYwjjry1LZ3w9jxUVyYnanXqi4J7AawoSxna2lwKRREB";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAra76UXw+A2rGVMSaatI4qUhXJflZFznoTxZJKbCkhSQs2IBYDQduyIrYoY8Lpn8f0FkGx8TopNTN7ro6FxP0wJYpcA68gIKywYX0S+FFH+4GNU42dl7rjSnQWEYyhz3DgK4coVdRFZ7x4oRcR4jXQAw1+okRlCSRaZ9zXRaYOOfK5MHFyf1lNn9fEwl5EaTRyt+RO6RKQjIHvTo6c3SIQq3lISiyTTk0GA3gIj1BH7/7ZZg5rvVWDj/CUkAtJu953JvfQe74hUy3/7jOTlpU/C46pc4i6x6Pqbm6fVxFqyI5BUn7A7dCiD/8DRFRFRjvv+9TVRWVpfjM+2mADycnCwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url="https://3046c5a4.r9.cpolar.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gmall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
